<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="会员昵称" prop="memberName">
        <el-input
          v-model="queryParams.memberName"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="请选择"
          end-placeholder="请选择"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >
          新增会员
        </el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="tableList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="ID" prop="id" width="120" /> -->
      <el-table-column
        label="会员昵称"
        prop="memberName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="头像" prop="memberPicture">
        <template slot-scope="scope">
          <el-image
            style="width: 60px; height: 60px"
            :src="scope.row.memberPicture"
            :preview-src-list="[scope.row.memberPicture]"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="手机号码" prop="phonenumber">
        <template slot-scope="scope">
          {{ formatTableValue(scope.row.phonenumber) }}
        </template>
      </el-table-column>
      <el-table-column label="累计消费">
        <template slot-scope="scope">
          {{ formatTableValue(scope.row.cumulativeMoney) }}
        </template>
      </el-table-column>
      <el-table-column label="账户余额" >
        <template slot-scope="scope">
          {{ formatTableValue(scope.row.accountBalance) }}
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" >
        <template slot-scope="scope">
          {{ selectDictLabel(dict.type.sys_user_sex, scope.row.memberSex) }}
        </template>
      </el-table-column>
      <el-table-column label="生日" prop="memberBirthday">
        <template slot-scope="scope">
          {{ formatTableValue(scope.row.memberBirthday) }}
        </template>
      </el-table-column>
      <el-table-column label="是否会员" align="center">
        <template slot-scope="scope">
          {{ scope.row.status == 0 ? "否" : "是" }}
        </template>
      </el-table-column>
      <el-table-column label="注册时间" prop="createTime"/>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button type="text" @click="goDetailPage(scope.row)"
            >账单明细</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
      custom-class="custom-dialog"
      @close="closeDialog" :close-on-click-modal="false"
    >
      <el-form
        ref="userRightsform"
        :model="form"
        label-width="100px"
        :rules="rules"
      >
        <el-form-item label="会员名称：" prop="memberName">
          <el-input v-model="form.memberName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="手机号：" prop="phonenumber">
          <el-input
            v-model="form.phonenumber"
            maxlength="11"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="性别：">
          <el-radio-group v-model="form.memberSex">
            <el-radio :label="0">男</el-radio>
            <el-radio :label="1">女</el-radio>
            <el-radio :label="2">未知</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生日：">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="form.memberBirthday"
            type="date"
            placeholder="选择生日"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="账户余额：">
          <el-input v-model="form.accountBalance" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="累计消费：">
          <el-input
            v-number-input
            v-model="form.cumulativeMoney"
            placeholder="请输入"
          />
        </el-form-item>
        <el-form-item label="头像：">
          <el-input v-model="form.memberPicture" v-show="false" />
          <ImageUpload
            :value="form.memberPicture"
            :limit="1"
            @input="handleFileSuccess"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submitForm('userRightsform')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getMemberList, addMember } from "@/api/system/member";
import { formatDate } from "@/utils/index";

export default {
  name: "UserManage",
  dicts: ["sys_user_sex"],
  data() {
    return {
      loading: false,
      ids: [],
      dateRange: [],
      tableList: [],
      total: 1,
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        memberName: undefined,
        phonenumber: undefined,
        startDate: undefined,
        endDate: undefined,
      },
      open: false,
      title: "",
      form: {
        memberName: "",
        memberPicture: "",
        memberSex: "",
        memberBirthday: "",
        accountBalance: "",
        cumulativeMoney: "",
        phonenumber: "",
      },
      rules: {
        memberName: [
          {
            required: true,
            message: "会员名称必填",
            trigger: "blur",
          },
        ],
        phonenumber: [
          {
            required: true,
            message: "手机号码不能为空",
            trigger: "blur",
          },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
        memberSex: [
          {
            required: true,
            message: "性别必选",
            trigger: "change",
          },
        ],
        memberBirthday: [
          {
            required: true,
            message: "生日必选",
            trigger: "change",
          },
        ],
        accountBalance: [
          {
            required: true,
            message: "账户余额必填",
            trigger: "blur",
          },
        ],
        cumulativeMoney: [
          {
            required: true,
            message: "累计消费必填",
            trigger: "blur",
          },
        ],
        memberPicture: [
          {
            required: true,
            message: "头像必传",
            trigger: "change",
          },
        ],
      },
    };
  },
  watch: {
    dateRange: {
      handler(newVal, oldVal) {
        console.log("newVal: ", newVal);
        if (newVal != null && newVal.length > 0) {
          this.queryParams.startDate = newVal[0];
          this.queryParams.endDate = newVal[1];
        } else {
          this.queryParams.startDate = undefined;
          this.queryParams.endDate = undefined;
        }
      },
    },
  },
  created() {
    this.getList();
  },
  methods: {
    handleAdd() {
      this.open = true;
      this.title = "新增会员";
      this.form = {
        memberName: "",
        memberPicture: "",
        memberSex: "",
        memberBirthday: "",
        accountBalance: "",
        cumulativeMoney: "",
        phonenumber: "",
      };
    },
    // 文件上传成功处理
    handleFileSuccess(response) {
      this.form.memberPicture = response;
    },
    closeDialog() {
      this.open = false;
      this.resetForm("userRightsform");
    },
    submitForm(formName) {
      this.$refs[formName].validate(async (valid, errorObj) => {
        if (valid) {
          addMember(this.form).then((res) => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "新增成功!",
              });
              this.getList();
              this.closeDialog();
            }
          });
        }
      });
    },
    goDetailPage(row) {
      this.$router.push({
        path: `/member/userManage/billingDetail`,
        query: { id: row.id, memberName: row.memberName },
      });
    },
    getList() {
      this.loading = true;
      getMemberList(this.queryParams).then((response) => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.roleId);
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/member/export",
        {
          ...this.queryParams,
        },
        `注册会员_${formatDate(new Date())}.xlsx`
      );
    },
  },
};
</script>
